package com.attendance.service;

import com.alibaba.fastjson.JSONObject;
import com.attendance.annotation.Supplement;
import com.attendance.annotation.type.OperationType;
import com.attendance.mapper.UserDetailsMapper;
import com.attendance.pojo.UserDetails;
import com.attendance.security.user.pojo.AbstractUserDetails;
import com.attendance.security.user.pojo.UserInfo;
import com.attendance.security.user.service.AbstractUserDetailsService;
import com.attendance.security.user.service.UserService;
import com.attendance.spring.SpringSecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户详细信息服务
 *
 * @author 鵺丶
 * @date 2021/03/12
 */
@Service
public class UserDetailsServiceImpl implements AbstractUserDetailsService {
    @Autowired
    UserDetailsMapper userDetailsMapper;
    @Autowired
    DepartmentServiceImpl departmentService;
    @Autowired
    UserService userService;

    public UserDetails selectById(Long id) {
        return userDetailsMapper.selectById(id);
    }

    public List<UserDetails> getUserDetailsList() {
        return userDetailsMapper.selectList(null);
    }

    public Page<UserDetails> getListPage(Integer pageNum, Integer limit) {
        Page<UserDetails> page = new Page<>(pageNum, limit);
        userDetailsMapper.selectPage(page, null);
        for (UserDetails userDetails : page.getRecords()) {
            userDetails.setRoleNames(userDetailsMapper.findRoleNameById(userDetails.getUId()));
            userDetails.setDepartName(userDetailsMapper.findDerparNameById(userDetails.getUId()));
        }
        return page;
    }

    public Page<UserDetails> getListPage(Integer pageNum, Integer limit, Long dId) throws Exception {
        List<Long> idsByParentId = departmentService.getIdsByParentId(dId);
        if (idsByParentId.size() == 0) {
            throw new Exception("该部门没有用户");
        }
        QueryWrapper<UserDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("u_id", idsByParentId);
        Page<UserDetails> page = new Page<>(pageNum, limit);
        userDetailsMapper.selectPage(page, queryWrapper);
        for (UserDetails userDetails : page.getRecords()) {
            userDetails.setRoleNames(userDetailsMapper.findRoleNameById(userDetails.getUId()));
            userDetails.setDepartName(userDetailsMapper.findDerparNameById(userDetails.getUId()));
        }
        return page;
    }

    public Page<UserDetails> getListPage(Integer pageNum, Integer limit, String searchParams, Long dId) throws Exception {
        List<Long> idsByParentId = departmentService.getIdsByParentId(dId);
        if (idsByParentId.size() == 0) {
            throw new Exception("该部门没有用户");
        }
        JSONObject parse = JSONObject.parseObject(searchParams);
        QueryWrapper<UserDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("u_id", idsByParentId);
        for (String key : parse.keySet()) {
            queryWrapper.like(key, parse.get(key));
        }
        Page<UserDetails> page = new Page<>(pageNum, limit);
        userDetailsMapper.selectPage(page, queryWrapper);
        for (UserDetails userDetails : page.getRecords()) {
            userDetails.setRoleNames(userDetailsMapper.findRoleNameById(userDetails.getUId()));
            userDetails.setDepartName(userDetailsMapper.findDerparNameById(userDetails.getUId()));
        }
        return page;
    }

    public Page<UserDetails> getListPage(Integer pageNum, Integer limit, String searchParams) {
        JSONObject parse = JSONObject.parseObject(searchParams);
        QueryWrapper<UserDetails> queryWrapper = new QueryWrapper<>();
        for (String key : parse.keySet()) {
            queryWrapper.like(key, parse.get(key));
        }
        Page<UserDetails> page = new Page<>(pageNum, limit);
        userDetailsMapper.selectPage(page, queryWrapper);
        for (UserDetails userDetails : page.getRecords()) {
            userDetails.setRoleNames(userDetailsMapper.findRoleNameById(userDetails.getUId()));
            userDetails.setDepartName(userDetailsMapper.findDerparNameById(userDetails.getUId()));
        }
        return page;
    }

    public Boolean deleteUsers(List<Integer> ids) {
        return ids.size() == userDetailsMapper.deleteBatchIds(ids);
    }

    public Boolean deleteUser(Long id) {
        return userDetailsMapper.deleteById(id) != 0;
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateUser(UserDetails userDetails) {
        return userDetailsMapper.updateById(userDetails) != 0;
    }

    @Supplement(type = OperationType.PUT)
    public Boolean addUser(UserDetails userDetails, UserInfo userInfo) throws Exception {
        try {
            userDetails.setUId(userService.register(userInfo));
            return userDetailsMapper.insert(userDetails) != 0;
        } catch (Exception ignored) {
        }
        throw new Exception("用户名重复");
    }

    public Boolean updatePass(String oldPassword, String newPassword) throws Exception {
        UserInfo userInfo = userService.selectByName(SpringSecurityUtils.getUserName());
        if (oldPassword.equals(newPassword)) {
            throw new Exception("新旧密码不能一致");
        }
        if (userService.matchPassword(userInfo, oldPassword)) {
            userInfo.setPassword(newPassword);
            return userService.updatePassword(userInfo);
        } else {
            throw new Exception("密码错误");
        }
    }

    @Override
    public AbstractUserDetails getUserDetailsForUser(UserInfo user) {
        QueryWrapper<UserDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id", user.getUserId());
        return userDetailsMapper.selectOne(queryWrapper);
    }
}
